package pri.zxx.excelgenerate.dataentity.tabledata.vitamin;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import pri.zxx.excelgenerate.dataentity.entity.*;
import pri.zxx.excelgenerate.dataentity.tabledata.MultiTitleModuleCreatorTemplate;
import pri.zxx.excelgenerate.dataentity.tabledata.TableDefinition;
import pri.zxx.excelgenerate.excelhanler.ExcelUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 妊娠母猪
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Component
public class VitaminModuleCreator extends MultiTitleModuleCreatorTemplate {


	//todo 注入数据获取的bean

	@Override
	public List<ModuleMetaInfo> creator(CreatRequest creatRequest) {
		CreatRequest.TableCreateReq tcr = new CreatRequest.TableCreateReq(2.0, 2, Arrays.asList("日粮需要量水平（%）", "日需要量（g/天）"));
		creatRequest.setTableCreateReq(tcr);

		List<ModuleMetaInfo> modules = new ArrayList<>();

		List<OpVitamin> unLoadData = new ArrayList<>();

		boolean loadRange = true;

		//todo 调用获取数据的service
		switch (creatRequest.getPredictionTypeEnum()) {
			case STARTING_PIGS:
				OpVitamin st = new OpVitamin();
				unLoadData.add(st);
				loadRange = false;
				break;
			case GESTATING_SOWS:
			case LACTATING_SOWS:
			case GROWING_FINISHING:
				OpVitamin op = new OpVitamin();
				OpVitamin op2 = new OpVitamin();
				OpVitamin op3 = new OpVitamin();
				OpVitamin op4 = new OpVitamin();
				op.setInitialBwforaa(BigDecimal.valueOf(1)).setFinalBwforaa(BigDecimal.valueOf(30)).setInitialDayinput(1).setFinalDayinput(30);
				op2.setInitialBwforaa(BigDecimal.valueOf(30)).setFinalBwforaa(BigDecimal.valueOf(50)).setInitialDayinput(30).setFinalDayinput(50);
				op3.setInitialBwforaa(BigDecimal.valueOf(50)).setFinalBwforaa(BigDecimal.valueOf(60)).setInitialDayinput(50).setFinalDayinput(60);
				op4.setInitialBwforaa(BigDecimal.valueOf(60)).setFinalBwforaa(BigDecimal.valueOf(80)).setInitialDayinput(60).setFinalDayinput(80);
				unLoadData.add(op);
				unLoadData.add(op2);
				unLoadData.add(op3);
				break;
			default:
				throw new RuntimeException("动物类型异常");

		}
		int tableCount = 1;
		List<CreatRequest.Range> ranges = null;
		if (loadRange) {
			ranges = creatRequest.getRanges();
			// 有范围要求的表格 根据选择的范围拆分 向上取整
			tableCount = (int) Math.ceil(ranges.size() / tcr.getMAX_REPEAT());//创建module数量
		}
		for (int j = 0; j < tableCount; j++) {

			ModuleMetaInfo metaInfo = new ModuleMetaInfo();
			TableDefinition tableDefinition = new TableDefinition();

			CreatRequest cr = new CreatRequest();
			if (loadRange) {
				List<CreatRequest.Range> rg = ranges.stream().skip((long) (tcr.getMAX_REPEAT() * j)).limit((long) tcr.getMAX_REPEAT()).collect(Collectors.toList());
				List<OpVitamin> pageData = unLoadData.stream().filter(opVitamin -> {
					//判断是体重范围 还是 时间范围
					Boolean isWeight = ExcelUtil.isWeight(creatRequest);
					String rangeStr;
					if (isWeight) {
						BigDecimal finalBwforaa = opVitamin.getFinalBwforaa();
						BigDecimal initialBwforaa = opVitamin.getInitialBwforaa();
						rangeStr = initialBwforaa.toString() + "-" + finalBwforaa.toString();
					} else {
						Integer initialDayinput = opVitamin.getInitialDayinput();
						Integer finalDayinput = opVitamin.getFinalDayinput();
						rangeStr = initialDayinput + "-" + finalDayinput;
					}
					return rg.stream().anyMatch(range -> rangeStr.equals(range.getStart() + "-" + range.getEnd()));
				}).collect(Collectors.toList());
				tableDefinition.setUnLoadData(pageData).setDataClass(OpVitamin.class);
				BeanUtils.copyProperties(creatRequest, cr);
				cr.setRanges(rg);

			} else {
				tableDefinition.setUnLoadData(unLoadData).setDataClass(OpVitamin.class);
				cr = creatRequest;
			}

			//组装数据
			tableDefinition = dataHandle(moduleMetaInfoHandle(tableDefinition, cr), cr);
			metaInfo.setTableDefinition(tableDefinition);
			String s = "";
			if (tableCount > 1) {
				s = JOIN_DECILITER + (j + 1);
			}
			metaInfo.setModuleTitle(moduleInfo().getName() + s).setModuleInfoEnum(moduleInfo());
			modules.add(metaInfo);
		}
		return modules;
	}

	@Override
	public ModuleInfoEnum moduleInfo() {
		return ModuleInfoEnum.VITAMIN;
	}

}
